package edu.odu.cs.sstore.repository;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import edu.odu.cs.sstore.domain.Authority;

@Repository
public class AuthorityRepositoryImpl implements AuthorityRepository {

	@Autowired(required=true)
	private SessionFactory sessionFactory;
	
	public Authority find(String role) {
		return (Authority) this.sessionFactory.getCurrentSession().createQuery(
			"from Authority auth where auth.authority=?").setParameter(0, role)
			.uniqueResult();
	}

	public Authority create(String role) {
		Authority authority = new Authority(role);
		this.sessionFactory.getCurrentSession().saveOrUpdate(authority);
		return authority;
	}

	public void delete(Authority authority) {
		this.sessionFactory.getCurrentSession().delete(authority);
	}

}
